package com.lcifn.stream;

import java.security.InvalidParameterException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Random;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;

import org.apache.tomcat.jni.User;
import org.junit.Test;

public class StreamApiTest {

	@Test
	public void testNaturalSupplier(){
		Stream<Long> stream = Stream.generate(new NaturalSupplier());
		stream.map((x) -> {
			return x * x;
		}).limit(10).forEach(System.out::println);
	}
	
	@Test
	public void testFibonacciSupplier(){
		Stream<Long> fibonacci = Stream.generate(new FibonacciSupplier());
		fibonacci.skip(20).limit(10).forEach(System.out::println);
	}
	
	@Test
	public void testX(){
		String[] strs = {"1", "2", "3"};
		Stream.of(strs);
		
		Stream<String> stream1 = Stream.of("1", "2", "3");
		Stream<String> stream2 = Stream.of("4", "5");
		Stream.concat(stream1, stream2);
		
		Stream.builder()
			.add("1")
			.add("2")
			.build();
		
		Stream.generate(new Supplier<Long>() {
			long value = 0;
			
			@Override
			public Long get() {
				this.value = this.value + 1;
				return this.value;
			}
		});
		
		boolean flag = IntStream.of(1,2,3).noneMatch(i -> i > 4);
		
		Stream<List<Integer>> intStream = Stream.of(Arrays.asList(1, 2, 3),Arrays.asList(4,5),Arrays.asList(6));
		intStream.flatMap(childList -> childList.stream()).forEach(System.out::println);
		
		List<Long> longList = null;
		//Optional.ofNullable(longList).orElseThrow(IllegalArgumentException::new);
		
		//longList.stream().map(l -> l + 1L).collect(Collectors.toList());
		
		List<Integer> numbers = Arrays.asList(1, null, 2);
		numbers.stream().mapToInt(n -> n * 2).sum();
		
		int[] nums = {1, 2, 3, 2};
		Arrays.stream(nums).distinct().forEach(System.out::println);
		
		//List<Integer> numbers = Arrays.asList(-1, 1, 0);
		//numbers.stream().filter(n -> n > 0).collect(Collectors.counting());
		
		
		Stream.of(1, 2, 3).map(n -> n + 1).collect(Collectors.toList());
		
		int sum = IntStream.range(1, 10).peek(System.out::println).sum();
		System.out.println("sum:" + sum);
		
	}
	
	@Test
	public void testY(){
		int sum = IntStream.range(1, 10).peek(System.out::println).sum();
		System.out.println("sum:" + sum);
		
		IntStream.range(1, 10).skip(3).limit(5).forEach(System.out::println);
		
		IntStream.range(1, 100).skip(10).limit(10).forEach(System.out::println);
	}
}
